package com.naiterui.ehp.bs.doctor.dao.user.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.google.common.collect.Lists;
import com.naiterui.ehp.bp.domain.Licence;
import com.naiterui.ehp.bs.doctor.dao.user.ILicenceDao;
import com.naiterui.ehp.bs.doctor.repository.ILicenceRepository;

/**
 * 医生认证信息
 *
 * @version 2015年6月24日 下午4:39:44 by chenlin
 * @Description 概述
 */
@Repository
public class AdLicenceDaoImpl implements ILicenceDao {

    @Autowired
    private ILicenceRepository licenceRepository;

    @Override
    public Licence getLicenceByType(long doctorId, int type) {
        List<Licence> licences = licenceRepository.findByDoctorIdAndType(doctorId, type);
        return (licences == null || licences.size() == 0) ? null : licences.get(0);
    }

    @Override
    public List<Licence> getLicenceByTypeAndNumber(int type, String number) {
        return licenceRepository.findAllByTypeAndNumber(type, number);
    }

    @Override
    public void saveOrUpdate(Licence licence) {
        if(licence.getId() == null){
            licenceRepository.save(licence);
        } else {
            licenceRepository.update(licence);
        }
    }

    @Override
    public Map<Integer, Licence> getLicenceByDoctorId(long doctorId, Integer... types) {

        // 查询资质
        List<Licence> licences = licenceRepository.findByDoctorIdAndTypeIn(doctorId, Lists.newArrayList(types));
        // map返回结果
        Map<Integer, Licence> result = new HashMap<>();
        for (Licence licence : licences) {
            if (licence == null) {
                continue;
            }
            result.put(licence.getType(), licence);
        }
        return result;

    }



}
